<?php
function add_relation($user_id, $token)
{
    $map = read_data();
    $map[$user_id] = $token;
    write_data(json_encode($map));
}
function get_token_by_shopid($shopid)
{
    $item=pdo_fetch("SELECT * FROM ".tablename('j_hmoney_elemtoken')." where shopid=:a",array(":a"=>$shopid));
    if (!$item) return null;
    return (Object)($item);
}
function read_data()
{
    $file_name = "token_relation.txt";
    $path = $file_name;
    if (!file_exists($path)) {
        throw new Exception("open " . $file_name . " error");
    }
    $file_content = file_get_contents($path);
    $map = json_decode($file_content, true);
    return $map;
}
function write_data($content)
{
    $file_name = "token_relation.txt";
    $path =  $file_name;
    $file = fopen($path, "w");
    fwrite($file, $content);
    fclose($file);
}
function load_page($file_name)
{
    $file_name = "page/" . $file_name;
    $path = $file_name;
    if (!file_exists($path)) {
        return ("open " . $file_name . " error");
    }
    $file_content = file_get_contents($path);
    return $file_content;
}

function format_price($price){
    return intval($price*100);
}
function create_uuid()
{
    return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
        mt_rand(0, 0xffff), mt_rand(0, 0xffff),
        mt_rand(0, 0xffff),
        mt_rand(0, 0x0fff) | 0x4000,
        mt_rand(0, 0x3fff) | 0x8000,
        mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
    );
}


function convert_to_message($content)
{
    try {
        $message = json_decode($content, true, 512, JSON_BIGINT_AS_STRING);
    } catch (Exception $e) {
        return("convert content to message error.");
    }
    return $message;
}

function check_signature($message, $secret)
{
    $params = $message;
    $signature = $message["signature"];
    unset($params["signature"]);
    if ($signature != get_signature($params, $secret)) {
        return false;
    }
    return true;
}

function get_signature(array $param, $secret)
{
    ksort($param);
    $string = "";
    foreach ($param as $key => $value) {
        $string .= $key . "=" . $value;
    }
    $splice = $string . $secret;
    $md5 = strtoupper(md5($splice));
    return $md5;
}

function wildDog_socket($shopid,$orderid){
    //$item=pdo_fetch("SELECT * FROM ".tablename('j_hmoney_elemtoken')." where shopid=:a",array(":a"=>$shopid));
    //$url=$item['wilddog']."/".$shopid."/.json";
    $url="https://wd5678249210wwwixs.wilddogio.com/".$shopid."/.json";
    $headers = array(
        'Accept: application/json',
        'Content-Type: application/json',
    );
    $postdata = json_encode(array(strval($orderid)=>TIMESTAMP));
    $handle = curl_init();
    curl_setopt($handle, CURLOPT_URL, $url); // 要访问的地址
    curl_setopt($handle,CURLOPT_HEADER,1); // 是否显示返回的Header区域内容
    curl_setopt($handle, CURLOPT_HTTPHEADER, $headers); //设置请求头
    curl_setopt($handle, CURLOPT_RETURNTRANSFER, true); // 获取的信息以文件流的形式返回
    curl_setopt($handle, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在
    curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false); // 对认证证书来源的检查
    curl_setopt($handle, CURLOPT_CUSTOMREQUEST, 'PATCH');
    curl_setopt($handle, CURLOPT_POSTFIELDS, $postdata); //设置请求体，提交数据包
    $response = curl_exec($handle); // 执行操作
    curl_close ($handle); // 关闭CURL会话
    return $response;
}
